/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.property.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.property.domain.Property1;
import me.zhengjie.modules.property.service.Property1Service;
import me.zhengjie.modules.property.service.dto.Property1QueryCriteria;
import me.zhengjie.modules.property.service.impl.Property1ServiceImpl;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author yyj
* @date 2021-10-01
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "property管理")
@RequestMapping("/api/property")
public class Property1Controller {

    private final Property1Service propertyService;
    private final Property1ServiceImpl propertyServiceImpl;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('property:list')")
    public void download(HttpServletResponse response, Property1QueryCriteria criteria) throws IOException {
        propertyService.download(propertyService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询property")
    @ApiOperation("查询property")
    @PreAuthorize("@el.check('property:list')")
    public ResponseEntity<Object> query(Property1QueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(propertyService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增property")
    @ApiOperation("新增property")
    @PreAuthorize("@el.check('property:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Property1 resources){
        return new ResponseEntity<>(propertyService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改property")
    @ApiOperation("修改property")
    @PreAuthorize("@el.check('property:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Property1 resources){
        propertyService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除property")
    @ApiOperation("删除property")
    @PreAuthorize("@el.check('property:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        propertyService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("导出信息模板")
    @ApiOperation("导出信息模板")
    @PreAuthorize("@el.check('property:list')")
    @GetMapping(value = "/export")
    public void exportTemplate(HttpServletResponse response, Property1 criteria) throws IOException {
        propertyService.exportTemplate(response);
    }


    @Log("批量导入信息")
    @ApiOperation("批量导入信息模板")
    @PreAuthorize("@el.check('property:import')")
    @PostMapping(value = "/upload")
    public Map<String, Object> upload(@RequestParam("file") MultipartFile file) throws Exception {
        return propertyService.upload(file);
    }

    @RequestMapping(value="/propertyNum")
    public long propertyNum(){
        return propertyService.propertyNum();
    }


    @AnonymousAccess
    @GetMapping(value="/sumProperty/{dateOne}")
    public Map<Object,Object> summaryKeyNum(@PathVariable Date dateOne){
        return propertyServiceImpl.sumProperty(dateOne);
    }

    @AnonymousAccess
    @GetMapping(value="/sumProperty")
    public List<Map<Object,Object>> number(){
        return propertyService.number();
    }

    @AnonymousAccess
    @GetMapping(value="/sumProperty1")
    public List<Map<Object,Object>> number1(){
        return propertyServiceImpl.number1();
    }

    @AnonymousAccess
    @GetMapping(value="/getPrice")
    public Integer getPrice(){
        return propertyService.getPrice();
    }
}